package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.SystemMessages;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SystemMessagesQueryObject;
import cn.wolfcode.trip.base.service.ISystemMessagesService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemmessages")
public class SystemMessagesController {

    @Autowired
    private ISystemMessagesService systemMessagesService;
    @Autowired
    private IUserService userService;
    //系统列表
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") SystemMessagesQueryObject qo, Model model){
        qo.setOrderBy("sm.createTime desc");
        model.addAttribute("pageInfo",systemMessagesService.query(qo));
        qo.setPageSize(0);
        qo.setKeyword(null);
        model.addAttribute("users",userService.query(qo));

        return "/systemmessages/list";
    }
    //发送系统消息
    @RequestMapping("save")
    @ResponseBody
    public Object save(SystemMessages systemMessages,Long[] ids){
        JSONResult result = new JSONResult();
        try {
            systemMessagesService.save(systemMessages,ids);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加出错");
        }
        return result;
    }
    //删除系统消息
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        JSONResult result = new JSONResult();
        try {
            systemMessagesService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除出错");
        }
        return result;
    }
}
